package com.zimin.cloud.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zimin.cloud.mine.pojo.wiki.business.CategoryBO;
import com.zimin.cloud.mine.pojo.wiki.view.CategoryVO;
import com.zimin.cloud.wiki.mapper.CategoryMapper;
import com.zimin.cloud.wiki.pojo.entity.Category;
import com.zimin.cloud.wiki.service.ICategoryService;
import com.zimin.common.utils.BeanUtils;
import com.zimin.common.utils.StringUtils;
import com.zimin.shared.pojo.PageVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author zimin
 * @since 2021-10-27 21:02
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public PageVO<CategoryVO> list(Page page, CategoryBO categoryBO) {
        Page<Category> categoryPage = page(page, Wrappers.<Category>lambdaQuery()
                .like(StringUtils.isNotEmpty(categoryBO.getName()), Category::getName, categoryBO.getName()));
        List<Category> records = categoryPage.getRecords();
        List<CategoryVO> categoryVOS = BeanUtils.listEntityToView(records, CategoryVO.class);
        return pageToPageVO(categoryPage, categoryVOS);
    }

    @Override
    public List<CategoryVO> list(CategoryBO categoryBO) {
        List<Category> list = list(Wrappers.<Category>lambdaQuery()
                .like(StringUtils.isNotEmpty(categoryBO.getName()), Category::getName, categoryBO.getName())
                .orderByAsc(Category::getSort));
        List<CategoryVO> categoryVOS = BeanUtils.listEntityToView(list, CategoryVO.class);
        return categoryVOS;
    }

    @Override
    public boolean save(CategoryBO categoryBO) {
        var category = new Category();
        BeanUtils.copyProperties(categoryBO, category);
        return save(category);
    }

    @Override
    public boolean updateById(CategoryBO categoryBO) {
        var category = new Category();
        BeanUtils.copyProperties(categoryBO, category);
        return updateById(category);
    }

    public Category getOne(CategoryBO categoryBO) {
        LambdaQueryWrapper<Category> wrapper = Wrappers.<Category>lambdaQuery()
                .eq(StringUtils.isNotEmpty(categoryBO.getName()), Category::getName, categoryBO.getName());
        return super.getOne(wrapper);
    }
}
